// pages/login/login.js
Page({
  data: {
    globalData: getApp().globalData,
    phone: '',
    password: ''
  },

  login() {
    wx.request({
      url: this.data.globalData.server + '/user/login',
      method: 'POST',
      data: {
        phone: this.data.phone,
        password: this.data.password
      },
      success(res) {
        console.log(res);
        let info = res.data
        if(info.code == 1) {
          wx.setStorageSync('user', info.data)
          wx.redirectTo({url: '/pages/my/my'})
        } else {
          wx.showModal({
            title: '登录失败',
            content: '账号或密码错误，请重试！',
            showCancel: false
          })
        }
      }
    })
  },

  register() {
    if(this.data.phone.length != 11) {
      wx.showToast({
        icon: 'error',
        title: '手机号格式错误',
      })
      return
    }
    let that = this
    wx.request({
      url: this.data.globalData.server + '/user',
      method: 'POST',
      data: {
        phone: this.data.phone,
        password: this.data.password
      },
      success(res) {
        let info = res.data
        if(info.code == 1) {
          wx.showToast({title: '可以登录啦'})
          that.setData({phone: '', password: ''})
        } else {
          console.log(res.data);
          wx.showModal({
            title: '错误',
            content: res.data.msg,
            showCancel: false
          })
        }
      }
    })
  }

})